package com.echo.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.echo.fragment.HistoryAllRecordsFragment;
import com.echo.fragment.HistoryChartsFragment;
import com.echo.fragment.HistoryChartsMonthFragment;
import com.echo.fragment.HistoryChartsWeekFragment;
import com.echo.fragment.HistoryPathFragment;

/**
 * Created by EchoGe on 2016/6/4.
 */
public class HistoryTabsAdapter extends FragmentPagerAdapter {

    private HistoryChartsFragment mWeekFragment,mMonthFragment,mRecordFragment;
    private HistoryPathFragment mPathFragment;
    private String[] mTitles;

    public HistoryTabsAdapter(FragmentManager fm, String[] titles){
        super(fm);
        mTitles=titles;
        //fragments=new Fragment[mTitles.length];
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment=null;
        switch (position){
            case 0:
                if(mWeekFragment==null) {
                    mWeekFragment = new HistoryChartsWeekFragment();
                }
                fragment=mWeekFragment;
                break;
            case 1:
                if(mMonthFragment==null) {
                    mMonthFragment = new HistoryChartsMonthFragment();
                }
                fragment=mMonthFragment;
                break;
            case 2:
                if(mRecordFragment==null){
                    mRecordFragment=new HistoryAllRecordsFragment();
                }
                fragment=mRecordFragment;
                break;
            case 3:
                if(mPathFragment==null){
                    mPathFragment=new HistoryPathFragment();
                }
                fragment=mPathFragment;
                break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return mTitles.length;
    }

    public CharSequence getPageTitle(int position){return mTitles[position];}
}
